#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Lee
# @File    : dhy_db_tool.py
# @Time    : 2024/1/30 16:55
import json
import sqlite3

# UserConfig
# sqlite_sequence
# WifiConfig
# VehicleConfig
# FaultConfig
# task_record
# base_line
#     (0, 'id', 'INTEGER', 1, None, 1)
#     (1, 'name', 'varchar(255)', 1, None, 0)
#     (2, 'task_type', 'INTEGER', 1, None, 0)
#     (3, 'line_type', 'INTEGER', 1, None, 0) AB-0, A+ - 1, 平行曲线 - 2, 圆曲线 - 3, 对角耙地 - 3
#     (4, 'points', 'TEXT', 1, None, 0)
#     (5, 'angle', 'varchar(255)', 1, None, 0)
#     (6, 'width_cloth', 'varchar(255)', 1, None, 0)
#     (7, 'creat_time', 'varchar(255)', 1, None, 0)
#     (8, 'update_time', 'varchar(255)', 1, None, 0)
#     (9, 'is_deleted', 'INTEGER', 1, '0', 0)
#     (10, 'lon_lats', 'TEXT', 1, "''", 0)
# UserSet

global null
null = 0


class DHYSqliteTool:
    def __init__(self, db_path="../data/NavigatorClient.db"):
        self.db_path = db_path
        self.conn = sqlite3.connect(db_path)
        self.cursor = self.conn.cursor()
        self.line_type_map = {
            "0": "AB线",
            "1": "A+线",
            "2": "平行曲线",
            "3": "圆曲线",
            "4": "对角耙地"
        }

    def query_all(self, query):
        self.cursor.execute(query)
        result = self.cursor.fetchall()
        for row in result:
            print(row)
        self.conn.close()

    def get_base_line_info(self):
        _result_list = list()
        query = "select line_type, name, points, angle from base_line where is_deleted = 0"
        try:
            self.cursor.execute(query)
            result = self.cursor.fetchall()
        except Exception as e:
            raise e
        else:
            for row in result:
                _line_type, _name, _points, _angle = row
                _task_dict = {
                    "task_type": self.line_type_map[str(_line_type)],
                    "task_name": _name,
                    "task_angle": float(_angle),
                    "task_points": eval(_points)
                }
                _result_list.append(_task_dict)
        finally:
            self.conn.close()
            return _result_list


if __name__ == '__main__':
    _sqlite = DHYSqliteTool()
    # print(_sqlite.get_base_line_info())
#     # # 获取所有表名称
#     # _query = "SELECT name FROM sqlite_master WHERE type='table';"
#
#     # # 查询表的字段
#     query = f"PRAGMA table_info(base_line)"
    query = "select line_type, name, points, angle from base_line where is_deleted = 0"
    # query = "select * from base_line where name = '202402281405140001'"
#     query = "select * from base_line where name = 'A+_SN_DONG'"
    _sqlite.query_all(query)

# #
# cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
#
# cursor.execute("select * from base_line")
# result = cursor.fetchall()
# for row in result:
#     print(row)
#
# # 关闭数据库连接
# conn.close()
